package cn.kgc.controller;

import cn.kgc.common.vo.Result;
import cn.kgc.emp.entity.Emp;
import cn.kgc.service.feign.EmpServiceFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/consumer")
public class EmpController {

    @Resource
    private EmpServiceFeign empServiceFeign;

    @GetMapping("/emp/list")
    public Result<List<Emp>> getEmpList(){
        //return restTemplate.getForObject(HOST_PRE+"/emp/list", Result.class);
        return empServiceFeign.getEmpList();
    }

    @GetMapping("/emp/{id}")
    public Result<Emp> getEmpById(@PathVariable("id") Integer id){
        return empServiceFeign.getEmpById(id);
    }

    @PostMapping("/emp")
    public Result<Emp> addEmp(@RequestBody Emp emp){
        return empServiceFeign.addEmp(emp);
    }

    @PutMapping("/emp")
    public Result<Emp> updateEmp(@RequestBody Emp emp){
        empServiceFeign.updateEmp(emp);
        return Result.success("更新成功");
    }

    @DeleteMapping("/emp/{id}")
    public Result<Emp> deleteEmpById(@PathVariable("id") Integer id){
        empServiceFeign.deleteEmpById(id);
        return Result.success("删除成功");
    }

    /*@Autowired
    private RestTemplate restTemplate;*/

    //private static final String HOST_PRE = "http://localhost:9999";
    //private static final String HOST_PRE = "http://HELLO-PROVIDER";

    /*@GetMapping("/emp/list")
    public Result<List<Emp>> getEmpList(){
        return restTemplate.getForObject(HOST_PRE+"/emp/list", Result.class);
    }

    @GetMapping("/emp/{id}")
    public Result<Emp> getEmpById(@PathVariable("id") Integer id){
        return restTemplate.getForObject(HOST_PRE+"/emp/"+id, Result.class);
    }

    @PostMapping("/emp")
    public Result<Emp> addEmp(@RequestBody Emp emp){
        return restTemplate.postForObject(HOST_PRE + "/emp", emp, Result.class);
    }

    @PutMapping("/emp")
    public Result<Emp> updateEmp(@RequestBody Emp emp){
        restTemplate.put(HOST_PRE + "/emp", emp);
        return Result.success("更新成功");
    }

    @DeleteMapping("/emp/{id}")
    public Result<Emp> deleteEmpById(@PathVariable("id") Integer id){
        restTemplate.delete(HOST_PRE+"/emp/"+id);
        return Result.success("删除成功");
    }*/
}
